@echo off

setlocal ENABLEDELAYEDEXPANSION

cd %~dp0
%~d0

set /p changesavepath=<savepath.txt
set changesavepath=%changesavepath:\=/%
echo|set /p="%changesavepath%">savepath.txt

ruby downloadTodays.rb


set "subslash=\"
set /p savepath= <savepath.txt

rem trim any slashes off the end
for /l %%a in (1,1,1) do if "!savepath:~-1!"=="/" set savepath=!savepath:~0,-1!
rem add slash to the end
set "savepath=%savepath%/"
rem change all slashes to windows
set savepath=%savepath:/=\%


set noquotessavepath=%savepath%
for /f "useback tokens=*" %%a in ('%noquotessavepath%') do set noquotessavepath=%%~a

set noslashpath=%savepath%
for /l %%a in (1,1,31) do if "!noslashpath:~-1!"=="\" set noslashpath=!noslashpath:~0,-1!

rem forfiles -p ^"%noslashpath%^" -s -m *.pdf /D -4 /C "cmd /c del @PATH"
rem echo %noslashpath%

rem how many pdf files in directory?
set /a count=0
set "searchfilter=%savepath%*.pdf"
set searchfilter=^"%searchfilter%^"
for %%f in (%searchfilter%) do (set /a count=count+1
rem echo %%~nf
)
rem @echo on
set /a num_of_manifest=%count%
set /a filecounter=0
set /a whilecounter=0
for /f "tokens=*" %%a in ('dir "pdfs\*.pdf" /O:D /s /b') do (
	set newest=%%a
	set /a filecounter=filecounter+1
	if NOT !filecounter!==%num_of_manifest% (set second_newest=%%a)
)


rem @echo off


set "param1=%second_newest%"
set "param2=%newest%"

set param1=^"%param1%^"
set param2=^"%param2%^"
rem echo.%param2%
set "tempsave=%noquotessavepath%trtemp.pdf"
set tempsave=^"%tempsave%^"


For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set currdate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set currtime=%%a%%b)


rem normalise creation date line and fix extra CRLF

set strtocheck=%param1%
for /f "useback tokens=*" %%a in ('%strtocheck%') do set strtocheck=%%~a
set "strtochecktemp=%strtocheck:.pdf=%"

If "%strtocheck%"=="%strtocheck:normalised=%" (
"D:\Program Files (x86)\GnuWin32\bin\sed" -b "s/CreationDate (D:.*)/CreationDate (D:20140101000000)/" %param1% > %tempsave%
move /Y %tempsave% %param1%

rem "D:\Program Files (x86)\GnuWin32\bin\tr" -s "/\r\r\n//" < %param1% > %tempsave%
rem move /Y %tempsave% %param1%

move "%strtocheck%" "%strtochecktemp%normalised.pdf"
set "second_newest=%strtochecktemp%normalised%currdate%%currtime%.pdf"
)


set strtocheck=%param2%
for /f "useback tokens=*" %%a in ('%strtocheck%') do set strtocheck=%%~a
set "strtochecktemp=%strtocheck:.pdf=%"

If "%strtocheck%"=="%strtocheck:normalised=%" (

"D:\Program Files (x86)\GnuWin32\bin\sed" -b "s/CreationDate (D:.*)/CreationDate (D:20140101000000)/" %param2% > %tempsave%
move /Y %tempsave% %param2%

rem "D:\Program Files (x86)\GnuWin32\bin\tr" -s "\r\r\n//" < %param2% > %tempsave%
rem move /Y %tempsave% %param2%
move "%strtocheck%" "%strtochecktemp%normalised%currdate%%currtime%.pdf"
set "newest=%strtochecktemp%normalised%currdate%%currtime%.pdf"
)

set "param1=%second_newest%"
set "param2=%newest%"

set param1=^"%param1%^"
set param2=^"%param2%^"

call:compare %param1% %param2% result

set "noman=%noquotessavepath%\nomanifest"
set noman="%noman%"

rem echo.result after : %result%

IF "%result%"=="same" (
set "subject=No changes to manifest"
set "body="
set "attachment=blah none"
)

IF "%result%"=="different" IF NOT "%result%"=="same" (
set "subject=Manifest change"
set "body=Manifest change found at %currtime%"
set "attachment=%newest%"
)

IF NOT "%result%"=="same" IF NOT "%result%"=="different" (
set "subject=erronous manifest check"
set "body="
set "attachment=blah none"
)

if exist %noman% (
set "subject=No manifests available for today"
set "body=Checked at %currdate% %currtime%"
set "attachment=blah none"
del %noman%
)

cscript /nologo sendmail.vbs "%subject%" "%body%" "%attachment%"
goto:eof


:compare
set /a result
for /f "skip=3 delims= " %%i in ('fciv.exe -md5 %1') do set md5_1=%%i
for /f "skip=3 delims= " %%i in ('fciv.exe -md5 %2') do set md5_2=%%i
rem echo %1
rem echo !md5_1!
rem echo %2
rem echo !md5_2!
if "!md5_1!"=="!md5_2!" (set "%~3=same") else (set "%~3=different")

set md5_1=
set md5_2=

goto:eof


endlocal